package org.linlinjava.litemall.admin.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallCoupon;
import org.linlinjava.litemall.db.service.LitemallConponUserService;
import org.linlinjava.litemall.db.service.LitemallCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin/coupon")
@Validated
public class AdminCouponController {

    @Autowired
    LitemallCouponService litemallCouponService;

    @Autowired
    LitemallConponUserService litemallConponUserService;

    @GetMapping("/list")
    public Object list(String name, Short type, Short status,
                       @RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){
        return ResponseUtil.okList(litemallCouponService.getAllBytitleOrTypeOrStatus(name,type,status,page,limit,sort, order));
    }

    @PostMapping("/create")
    public Object create(@RequestBody LitemallCoupon coupon){
        boolean result = litemallCouponService.add(coupon);
        if(result){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/update")
    public Object update(@RequestBody LitemallCoupon coupon){
        if(coupon.getTag().equals("仅兑换领券")){

        }
        boolean result = litemallCouponService.modify(coupon);
        if(result){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallCoupon coupon){
        boolean result = litemallCouponService.delete(coupon);
        if(result){
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(Integer id){
        return ResponseUtil.ok(litemallCouponService.getCouponById(id));
    }

    @RequiresPermissions("admin:coupon:listuser")
    @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "查询用户")
    @GetMapping("/listuser")
    public Object listuser(Integer userId, Integer couponId, Short status,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit,
                           @RequestParam(defaultValue = "add_time") String sort,
                           @RequestParam(defaultValue = "desc") String order){
        return ResponseUtil.okList(litemallConponUserService.getAll(userId,page,limit,status,couponId,sort,order));
    }


}
